<template>
  <view class="container">
    <uni-forms ref="form" :modelValue="formData" labelWidth="120px" labelPosition="top">
      <cu-cell-group>
        <uni-forms-item name="managerType">
          <cu-cell title="检查表" required link @click="checkListClick()">
            <image
              v-if="isCheckSign"
              slot="default"
              class="icon-style"
              src="/static/images/notOverdueComplete.png"
              mode="aspectFit"
            />
          </cu-cell>
        </uni-forms-item>
        <uni-forms-item name="managerType">
          <cu-cell title="程序表" required link @click="procedureClick()">
            <image
              v-if="isProcedureSign"
              slot="default"
              class="icon-style"
              src="/static/images/notOverdueComplete.png"
              mode="aspectFit"
            />
          </cu-cell>
        </uni-forms-item>
        <uni-forms-item>
          <cu-cell title="应急预案" description="查看" @click="emergencyClick()"></cu-cell>
        </uni-forms-item>
        <uni-forms-item>
          <cu-cell title="安全教育" description="查看" @click="safetyClick()"></cu-cell>
        </uni-forms-item>
      </cu-cell-group>
      <view class="submit-group">
        <button class="u-button" type="primary" :ripple="true" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>
<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import CuCell from '@/components/cu-cell/cu-cell.vue'
import { getWorkDetail, workSubmit } from '@/api/dock/trucks'
import { bscDockWorkSignObj } from '@/utils/constant.js'
export default {
  components: {
    CuCellGroup,
    CuCell
  },
  data() {
    return {
      bscDockWorkSignObj,
      formData: { subWorkId: '', createPersonSignReqs: [] },
      detail: {},
      subWorkId: '',
      isCheckSign: false,
      isProcedureSign: false
    }
  },
  onLoad(option) {
    this.subWorkId = option.subWorkId
    this.formData.subWorkId = this.subWorkId
    getWorkDetail(this.subWorkId).then(({ data }) => {
      this.detail = data.dockWorkInfoResp.formType === '1' ? data.dockFormLoadingResp : data.dockFormUnloadResp
    })
  },
  methods: {
    checkListClick() {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/ViewCheckList?subWorkId=${this.subWorkId}`,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {
            if (data && data.createPersonSign) {
              this.isCheckSign = true
              let result = this.formData.createPersonSignReqs.find(
                (item) => item.signObject === bscDockWorkSignObj.CHECKLIST
              )
              if (result) {
                result.signUrl = data.createPersonSign.signUrl
              } else {
                this.formData.createPersonSignReqs.push(data.createPersonSign)
              }
            }
          }
        }
      })
    },
    procedureClick() {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/ViewProcedureList?subWorkId=${this.subWorkId}`,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {
            if (data && data.createPersonSign) {
              this.isProcedureSign = true
              let result = this.formData.createPersonSignReqs.find(
                (item) => item.signObject === bscDockWorkSignObj.SCHEDULELIST
              )
              if (result) {
                result.signUrl = data.createPersonSign.signUrl
              } else {
                this.formData.createPersonSignReqs.push(data.createPersonSign)
              }
            }
          }
        }
      })
    },
    emergencyClick() {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/ViewEmergency?url=${this.detail.emergencyPlanFileUrl}&readonly=true&subWorkId=${this.subWorkId}`,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    safetyClick() {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/ViewSafety?url=${this.detail.safetyEducationFileUrl}&readonly=true&subWorkId=${this.subWorkId}`,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    submit() {
      if (!this.isCheckSign) {
        this.$modal.showToast('检查表还未确认签名！')
        return
      }
      if (!this.isProcedureSign) {
        this.$modal.showToast('程序表还未确认签名！')
        return
      }
      workSubmit(this.formData).then(() => {
        this.$modal.showToast('提交成功')
        uni.navigateBack()
      })
    }
  }
}
</script>
<style lang="scss">
.container {
  .submit-group {
    background: #fff;
    z-index: 9;
    padding: 30px;
  }
  .icon-style {
    width: 46rpx;
    height: 46rpx;
  }
}
</style>
